<?php
session_save_path("/tmp");
session_start();
/*
 *      zendo.php
 *
 *      Copyright 2010 Nolan Baker <hendersonhasselbalch@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>Text Zendo</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta name="generator" content="Geany 0.18" />
</head>

<body background='fore.png'>
<?php

########################################################################
# IMPORTS AND CONNECTIONS AND SUCH
########################################################################
include("func.php");
$con = mysql_connect("localhost", "nbaker", "");
mysql_query("use zendo;", $con);


########################################################################
# INITIALIZE THE GAME
########################################################################

$temp_word = "";
$errors = "";

if ($_POST){
    if (isset($_POST['name'])){
        $_SESSION['user'] = $_POST['name'];
        $_SESSION['last_word'] = "";
        
        # the player starts off with 0 guessing stones
        $_SESSION['stones'] = 0;

        # and is on the first word
        $_SESSION['order'] = 0;

        # for guessing
        $_SESSION['new_words'] = 1;

        # choose a rule
        $result = mysql_query("SELECT rule_id FROM rules ORDER BY Rand() LIMIT 1;", $con);
        while ($row = mysql_fetch_array($result)) {
            $_SESSION['rule'] = $row['rule_id'];
            mysql_query("INSERT INTO games (player_name, rule_id, date, time) VALUES ('"
                        .$_POST['name']."',".$row['rule_id'].", CURDATE(), CURTIME());", $con);
        }

        # then store the current game id as session data
        $game_id = mysql_query("SELECT MAX(game_id) as g FROM games;", $con);
        while ($row = mysql_fetch_array($game_id)) {
            $_SESSION['game'] = $row['g'];
        }

        # randWord takes in 2 args (rule, buddha nature)
        $b = randWord($_SESSION['rule'], 1);
        addWord($b,1);
        $n = randWord($_SESSION['rule'], 0);
        addWord($n,0);

    }

########################################################################
# ADD WORD
########################################################################
    if (isset($_POST['type'])) {
        if ($_POST['type'] == 'add') {
            $word = strtolower($_POST['word']);
            $is_word = isWord($word);
            if (! $is_word) {
                $errors .= "You must use an English word.\n";
            }
            elseif (! isset($_POST['buddha'])) {
                $errors .= "You must select either 'buddha' or 'not buddha'.\n";
                $temp_word = $word;
            }

            # check to see if the word has been used
            elseif (used($word, $_SESSION['game'])) {
                $errors .= "You've already used that word.\n";
            }
            else {
                $_SESSION['order'] += 1;
                $nature = isBuddha($word, $_SESSION['rule']);
                $correct = 0;
                if ($nature == $_POST['buddha']) {
                    $correct = 1;
                    $_SESSION['stones'] += 1;
                }
                addWord($word, $nature, $correct, $_SESSION['order']);
                $_SESSION['last_word'] = $word;
            }
        }
    }
}

########################################################################
# PAGE TOP
########################################################################
echo $_SESSION['user']." | <a href='help.php'>Help<a/><hr>";

########################################################################
# TABLE OF WORDS
########################################################################
print "<div align=center>";
print "<table border=1 cellpadding=5>";
print "<tr><td><font color='green'><big>Buddha</big></font></td>
      <td><font color='red'><big>Not Buddha</big></font></td></tr>";

$count = 0;

print "<tr><td valign='top'>"; # buddha words
$array = mysql_query("SELECT word FROM word_game WHERE game_id = "
                     .$_SESSION['game']." AND buddha = 1;", $con);

while ($row = mysql_fetch_array($array)) {
	$count++;
    if ($row['word'] == $_SESSION['last_word']) {
        print "<font color='blue'>".$row['word']."</font><p>";
    }
    else {
        print $row['word']."<p>";
    }
}

print "</td><td valign='top'>"; # non buddha words
$array = mysql_query("SELECT word FROM word_game WHERE game_id = "
                     .$_SESSION['game']." AND buddha = 0;", $con);

while ($row = mysql_fetch_array($array)) {
	$count++;
    if ($row['word'] == $_SESSION['last_word']) {
        print "<font color='blue'>".$row['word']."</font><p>";
    }
    else {
        print $row['word']."<p>";
    }
}
$_SESSION['num_moves'] = $count;

########################################################################
# GUESSING STONES
########################################################################
print "</td></tr></table><p>
      <table border=0 cellpadding=5>
      <tr><td>".$_SESSION['stones']." guessing stones</td>";

if ($_SESSION['stones'] > 0) {
    print "<td><form action='guess.php' method='post'>
         <input type='hidden' name='type' value='guess'>
         <input type='submit' value='guess rule'>
         </form></td>";
}

########################################################################
# CHECK BUDDHA NATURE OF WORD
########################################################################
print "</tr><tr><td><form action='zendo.php' method='post'>
     <input type='hidden' name='type' value='add'>
     <input type='text' name='word' value='".$temp_word."'><br>
     <input type='radio' name='buddha' value='1'> <font color='green'>buddha</font>
     <input type='radio' name='buddha' value='0'> <font color='red'>not</font> </td>
     <td><input type='submit' value='check word'>
     </form></td>";

print "</tr></table>";
print $errors."</div>";
?>
</body>
</html>
